/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.uerj.jms.banco;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Felipe Ribeiro
 */
public class PublishBanco {

    public static void start(String mensagem) throws NamingException, JMSException {

        enviarMensagem(mensagem);
    }

    private static void enviarMensagem(String mensagem) throws NamingException, JMSException {

        InitialContext ic = new InitialContext();
        TopicConnectionFactory factory = (TopicConnectionFactory) ic.lookup("jms/Fabrica");
        Topic topic = (Topic) ic.lookup("jms/tabelas");
        TopicConnection connection = factory.createTopicConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(topic);
        TextMessage message = session.createTextMessage();
        message.setStringProperty("modo", "resposta");
        message.setText(mensagem);
        sender.send(message);
        sender.close();
        session.close();
        connection.close();

    }
}
